﻿$(function() {
	InitLeftMenu();
	tabClose();
	tabCloseEven();
})

// 初始化左侧
function InitLeftMenu() {

	$("#menu").empty();
	var menulist = "";

	$.each(_menus.menus, function(i, n) {
		$("#menu").accordion(
				"add",
				{
					title : n.menuname,
					// click:GetSmallMenu(n.MenuID,n.MenuName),
					iconCls : n.icon,
					selected : false,
					content : '<div id="' + n.menuid + '"title="' + n.menuname
							+ '"  data-options="iconCls:\'' + n.icon
							+ '\'" style="overflow:auto;background-color:#15263f;"></div>'
				});

		// menulist += '<div title="'+n.menuname+'"
		// data-options="iconCls:\''+n.icon+'\'" style="overflow:auto;">';
		menulist += '<ul>';
		$.each(n.menus, function(j, o) {
			menulist += '<li><div><a href="javascript:void(0)" url="' + o.url
					+ '" ><span class="icon1 ' + o.icon + '" ></span>'
					+ o.menuname + '</a><span class="selectedspan"></span></div></li> ';
		})
		menulist += '</ul></div>';
		$('div[id=' + n.menuid + ']').append(menulist);
		menulist = "";
	});

	$('#menu').accordion({
		autoHeight : false,
		navigator : true
	});

	$('#menu li div').click(function() {
		var tabTitle = $(this).children("a").text();
		var url = $(this).children("a").attr("url");
		addTab(tabTitle, url);
		$('#menu li div').removeClass("selected");
		$(this).addClass("selected");
		$(".tabs-close").hover(function(){
			$(this).siblings("a.tabs-inner").addClass("cur");
		},function(){
			$(this).siblings("a.tabs-inner").removeClass("cur");
		});
		//tabs切换栏特效
		var boxbig=$('ul.tabs')[0];
		var but=$('ul.tabs li.tabs-selected')[0];
		tabLineRefresh(boxbig,but);
	});
	
	$('#tabs').tabs({
		onSelect : function(title){
			$('#tabs').tabs('select', title);
			var panels = $('#menu').accordion('panels');
			for(var i=0;i<panels.length;i++){
				if(panels[i][0].innerText.indexOf(title) != -1){
					$('#menu').accordion('select', i);
					$('#menu li div').removeClass("selected");
					var lipanel = $($("#menu div.panel")[i]).children("div.panel-body").find("a");
					for(var a=0;a<lipanel.length;a++){
						if($(lipanel[a]).text()==title){
							$(lipanel[a]).parent("div").addClass("selected");
							break;
						}
					}
					break;
				}
			}
		}
	});

}
function tabLineRefresh(bigbox,butbox){
				var boxbigleft=bigbox.offsetLeft;//获取到大盒子左边的宽度
				var butwidth=butbox.offsetWidth;
				var butleft=butbox.offsetLeft;
				var origionleft=butleft-180;
				var boxsmall;
				bigbox.style.borderColor='#f7f7f7';
				if(bigbox.getElementsByTagName('div')[0]==undefined){//判断是否要重复创建覆盖的盒子
				boxsmall=document.createElement('div');
				boxsmall.setAttribute('id','cover');
				boxsmall.style.width=butwidth+"px";
				boxsmall.style.height="2px";
				boxsmall.style.backgroundColor='#1374e0';
				bigbox.appendChild(boxsmall);
				boxsmall.style.position="absolute";
				boxsmall.style.top="37px";
				boxsmall.style.left=origionleft+"px";
				boxsmall.style.animation="longer 0.3s linear forwards";
			}
			else{
				boxsmall=bigbox.getElementsByTagName('div')[0];
				bigbox.removeChild(boxsmall);
				boxsmall=document.createElement('div');
				boxsmall.setAttribute('id','cover');
				boxsmall.style.width=butwidth+"px";
				boxsmall.style.height="2px";
				boxsmall.style.backgroundColor='#1374e0';
				bigbox.appendChild(boxsmall);
				boxsmall.style.position="absolute";
				boxsmall.style.top="37px";
				boxsmall.style.left=origionleft+"px";
				boxsmall.style.animation="longer 0.3s linear forwards";
		}
		}
function addTab(subtitle, url) {
	if (!$('#tabs').tabs('exists', subtitle)) {
		$('#tabs').tabs('add', {
			title : subtitle,
			content : createFrame(url),
			closable : true,
			width : $('#mainPanle').width() - 10,
			height : $('#mainPanle').height() - 26
		});
		//TABS切换栏特效
		
	} else {
		$('#tabs').tabs('select', subtitle);
	}
	tabClose();
}

function createFrame(url) {
	var s = '<iframe name="mainFrame" scrolling="auto" frameborder="0"  src="'
			+ url + '" style="width:99%;height:99%;"></iframe>';
	return s;
}

function tabClose() {
	/* 双击关闭TAB选项卡 */
	$(".tabs-inner").dblclick(function() {
		var subtitle = $(this).children("span").text();
		$('#tabs').tabs('close', subtitle);
	})

	$(".tabs-inner").bind('contextmenu', function(e) {
		$('#mm').menu('show', {
			left : e.pageX,
			top : e.pageY,
		});

		var subtitle = $(this).children("span").text();
		$('#mm').data("currtab", subtitle);

		return false;
	});
}
// 绑定右键菜单事件
function tabCloseEven() {
	// 关闭当前
	$('#mm-tabclose').click(function() {
		var currtab_title = $('#mm').data("currtab");
		$('#tabs').tabs('close', currtab_title);
	})
	// 全部关闭
	$('#mm-tabcloseall').click(function() {
		$('.tabs-inner span').each(function(i, n) {
			var t = $(n).text();
			if (t != "欢迎使用") {
				$('#tabs').tabs('close', t);
			}
		});
	});
	// 关闭除当前之外的TAB
	$('#mm-tabcloseother').click(function() {
		var currtab_title = $('#mm').data("currtab");
		$('.tabs-inner span').each(function(i, n) {
			var t = $(n).text();
			if (t != currtab_title)
				$('#tabs').tabs('close', t);
		});
	});
	// 关闭当前右侧的TAB
	$('#mm-tabcloseright').click(function() {
		var nextall = $('.tabs-selected').nextAll();
		if (nextall.length == 0) {
			// msgShow('系统提示','后边没有啦~~','error');
			alert('后边没有啦~~');
			return false;
		}
		nextall.each(function(i, n) {
			var t = $('a:eq(0) span', $(n)).text();
			$('#tabs').tabs('close', t);
		});
		return false;
	});
	// 关闭当前左侧的TAB
	$('#mm-tabcloseleft').click(function() {
		var prevall = $('.tabs-selected').prevAll();
		if (prevall.length == 0) {
			alert('到头了，前边没有啦~~');
			return false;
		}
		prevall.each(function(i, n) {
			var t = $('a:eq(0) span', $(n)).text();
			$('#tabs').tabs('close', t);
		});
		return false;
	});

	// 退出
	$("#mm-exit").click(function() {
		$('#mm').menu('hide');
	})
}

// 弹出信息窗口 title:标题 msgString:提示信息 msgType:信息类型 [error,info,question,warning]
function msgShow(title, msgString, msgType) {
	$.messager.alert(title, msgString, msgType);
}

function clockon() {
	var now = new Date();
	var year = now.getFullYear(); // getFullYear getYear
	var month = now.getMonth();
	var date = now.getDate();
	var day = now.getDay();
	var hour = now.getHours();
	var minu = now.getMinutes();
	var sec = now.getSeconds();
	var week;
	month = month + 1;
	if (month < 10)
		month = "0" + month;
	if (date < 10)
		date = "0" + date;
	if (hour < 10)
		hour = "0" + hour;
	if (minu < 10)
		minu = "0" + minu;
	if (sec < 10)
		sec = "0" + sec;
	var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
	week = arr_week[day];
	var time = "";
	time = year + "年" + month + "月" + date + "日" + " " + hour + ":" + minu
			+ ":" + sec + " " + week;

	$("#bgclock").html(time);

	var timer = setTimeout("clockon()", 200);
}
